/*
 * @Author: your name
 * @Date: 2024-05-07 23:31:48
 * @LastEditTime: 2024-05-07 23:59:11
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\216.组合总和-iii.cpp
 */
/*
 * @lc app=leetcode.cn id=216 lang=cpp
 *
 * [216] 组合总和 III
 */

// @lc code=start
class Solution {
public:
    vector<vector<int>>res;
    vector<int>path;
    void backtracking(int k, int n, int sum, int start_index){
        // 终止条件：path的长度等于k了
        if(path.size() == k){
            if(sum == n){
                // 满足K个数相加为n
                res.push_back(path);
            }
            return;
        }
        for(int i = start_index; i <= 9 - (k-path.size())+1; i++){
            if(sum + i > n) return;
            sum += i;
            path.push_back(i);
            backtracking(k, n, sum, i+1);
            sum -= i;
            path.pop_back();
        }
    }
    vector<vector<int>> combinationSum3(int k, int n) {
        backtracking(k, n, 0, 1);
        return res;
    }
};
// @lc code=end

